<template>
    <div class="app-container">
        <el-card class="main-card">
            <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="90px" size="small">
                <el-form-item label="分配用户名" prop="nickName">
                    <el-input v-model="queryParams.nickName" clearable placeholder="请输入分配用户名" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="组织名称" prop="deptName">
                    <el-input v-model="queryParams.deptName" clearable placeholder="请输入组织名称" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="模型名称" prop="modelName">
                    <el-input v-model="queryParams.modelName" clearable placeholder="请输入模型名称" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button v-hasPermi="['ghxx:bDeviceModel:list']" icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                    <el-button v-hasPermi="['ghxx:bDeviceModel:list']" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card">
            <el-row :gutter="10" class="mb20">
                <el-col :span="1.5">
                    <el-button v-hasPermi="['ghxx:bDeviceModel:add']" icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd">新增</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button v-hasPermi="['ghxx:bDeviceModel:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete">删除</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table class="base-table" :border="false" v-loading="loading" :data="bDeviceModelList" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="55" />
                <el-table-column align="center" label="分配用户名" prop="userName" />
                <el-table-column align="center" label="分配用户名" prop="nickName" />
                <el-table-column align="center" label="组织名称" prop="deptName" />
                <el-table-column align="center" label="模型名称" prop="modelName" />
                <el-table-column align="center" label="缩略图" prop="imageUrl" width="100">
                    <template slot-scope="scope">
                        <image-preview :height="25" :src="scope.row.imageUrl" :width="25" />
                    </template>
                </el-table-column>
                <el-table-column align="center" label="模型状态" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                    <template slot-scope="scope">
                        <el-button v-hasPermi="['ghxx:bDeviceModel:edit']" icon="el-icon-edit" type="primary" plain style="padding: 5px; margin: 0" size="small" @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button v-hasPermi="['ghxx:bDeviceModel:list']" icon="el-icon-video-play" type="primary" plain style="padding: 5px; margin: 0" size="small" @click="handlePreview(scope.row)">预览</el-button>
                        <el-button v-hasPermi="['ghxx:bDeviceModel:update']" icon="el-icon-delete" type="danger" plain style="padding: 5px; margin: 0" size="small" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />
        </el-card>
        <!-- 添加或修改三维配置对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                <el-form-item label="分配用户" prop="userName">
                    <el-input v-model="form.userName" :disabled="form.id != undefined" placeholder="请输入用户名" readonly>
                        <el-button slot="append" icon="el-icon-search" @click="selectUsernameClick"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item label="组织名称" prop="deptName">
                    <el-input v-model="form.deptName" placeholder="请输入组织名称" />
                </el-form-item>
                <el-form-item label="模型名称" prop="modelName">
                    <el-input v-model="form.modelName" placeholder="请输入模型名称" />
                </el-form-item>
                <el-form-item label="模型地址" prop="modelUrl">
                    <el-input v-model="form.modelUrl" placeholder="请输入模型地址" />
                </el-form-item>
                <el-form-item label="缩略图" prop="imageUrl">
                    <image-upload v-model="form.imageUrl" />
                </el-form-item>
                <el-form-item label="模型状态">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in dict.type.sys_show_hide" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog :close-on-click-modal="false" :visible.sync="selectUserDialog" append-to-body title="分配用户" width="50%">
            <SelectUser ref="selectUser" />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="selectUserConfirm">确 定</el-button>
                <el-button @click="selectUserDialog = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBDeviceModel, getBDeviceModel, delBDeviceModel, addBDeviceModel, updateBDeviceModel } from '@/views/zutai/api/zutai/bDeviceModel';
import SelectUser from '../bShortMessage/selectUser';

export default {
    name: 'BDeviceModel',
    dicts: ['sys_show_hide'],
    components: {
        SelectUser,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 三维配置表格数据
            bDeviceModelList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderByColumn: 'id',
                isAsc: 'desc',
                nickName: null,
                deptName: null,
                modelName: null,
                modelUrl: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                userName: [{ required: true, message: '分配用户名不能为空', trigger: 'blur' }],
                nickName: [{ required: true, message: '分配用户名不能为空', trigger: 'blur' }],
                deptName: [{ required: true, message: '组织名称不能为空', trigger: 'blur' }],
                modelName: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
                modelUrl: [{ required: true, message: '模型地址不能为空', trigger: 'blur' }],
                imageUrl: [{ required: true, message: '请上传缩略图', trigger: 'blur' }],
                status: [{ required: true, message: '是否弃用不能为空', trigger: 'blur' }],
            },
            selectUserDialog: false,
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handlePreview(row) {
            window.open(row.modelUrl);
        },
        selectUsernameClick() {
            this.selectUserDialog = true;
        },
        selectUserConfirm() {
            let selectData = this.$refs.selectUser.selectUserConfirm();
            console.log('选中的记录', selectData);
            if (selectData.length > 1) {
                this.$message({
                    message: '至多选择一条记录',
                    type: 'success',
                });
            } else {
                this.form.userName = selectData[0].userName;
                this.form.nickName = selectData[0].nickName;
                this.form.deptName = selectData[0].dept.deptName;
                this.selectUserDialog = false;
            }
        },
        /** 查询三维配置列表 */
        getList() {
            this.loading = true;
            listBDeviceModel(this.queryParams).then((response) => {
                this.bDeviceModelList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                userName: null,
                nickName: null,
                deptName: null,
                modelName: null,
                modelUrl: null,
                status: 0,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加三维配置';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getBDeviceModel(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = '修改三维配置';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBDeviceModel(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addBDeviceModel(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除三维配置编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delBDeviceModel(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'ghxx/bDeviceModel/export',
                {
                    ...this.queryParams,
                },
                `bDeviceModel_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
